/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'dart:async';

import 'package:flutter/services.dart';

/// Device Orientation API
/// 使用原生的旋转方法
class DeviceOrientationApi {
  static const _packageName = 'hadss_adaptive_video';
  static const MethodChannel _channel = MethodChannel(_packageName);
  static const EventChannel _gravityOrientationChannel =
      EventChannel('$_packageName/gravity_orientation');
  static Stream<int>? _gravityOrientationEvents;
  static const EventChannel _screenOrientationChannel =
      EventChannel('$_packageName/screen_orientation');
  static Stream<int>? _screenOrientationEvents;

  static Future<bool> isSensorAvailable() async {
    try {
      return await _channel.invokeMethod('isSensorAvailable');
    } on MissingPluginException catch (_) {
      return false;
    }
  }

  static setGravityUpdateInterval(int interval) async {
    await _channel
        .invokeMethod('setGravityUpdateInterval', {'interval': interval});
  }

  static Stream<int> get gravityOrientation {
    _gravityOrientationEvents ??= _gravityOrientationChannel
        .receiveBroadcastStream()
        .map((event) => event as int);
    return _gravityOrientationEvents!;
  }

  static Stream<int> get screenOrientation {
    _screenOrientationEvents ??= _screenOrientationChannel
        .receiveBroadcastStream()
        .map((event) => event as int);
    return _screenOrientationEvents!;
  }

  /// 临时旋转到竖屏，之后跟随传感器自动旋转，受控制中心的旋转开关控制，且旋转方向受系统判断
  static setPortraitUser() async {
    try {
      await _channel.invokeMethod('setPortraitUser');
    } on MissingPluginException catch (_) {
      return;
    }
  }

  /// 仅竖屏显示
  static setPortraitUpMode() async {
    try {
      await _channel.invokeMethod('setPortraitUp');
    } on MissingPluginException catch (_) {
      return;
    }
  }

  /// 临时旋转到横屏，之后跟随传感器自动旋转，受控制中心的旋转开关控制，且旋转方向受系统判断
  static setLandscapeLeftUser() async {
    try {
      await _channel.invokeMethod('setLandscapeLeftUser');
    } on MissingPluginException catch (_) {
      return;
    }
  }

  /// 临时旋转到反向横屏，之后跟随传感器自动旋转，受控制中心的旋转开关控制，且旋转方向受系统判断
  static setLandscapeRightUser() async {
    try {
      await _channel.invokeMethod('setLandscapeRightUser');
    } on MissingPluginException catch (_) {
      return;
    }
  }

  /// 跟随传感器自动旋转，受控制中心的旋转开关控制，且旋转方向受系统判断。(某种设备，无法旋转到反向竖屏)
  static setAutoOrientationUser() async {
    try {
      await _channel.invokeMethod('setAutoOrientationUser');
    } on MissingPluginException catch (_) {
      return;
    }
  }
}
